package com.jersey;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "deal")
public class DealBean {
	public static final SimpleDateFormat UI_DATE_FORMAT = new SimpleDateFormat(
			"MM/dd/yyyy HH:mm");
	
	private Integer dealID;

	@XmlElement(name = "merchant")
	private MerchantBean merchantBean;

	private Integer dealEngineID;

	private Integer deDealID;

	private String title;

	private Double price;

	private Double value;

	private String startDate;

	private String endDate;

	private String description;

	private String description2;

	private String providerLogo;

	private String finePrint;

	private String imageLink;

	private Integer applicableMarketID;

	private String applicableMarketName;

	@XmlElement(name = "dealCategory")
	private DealCategoryBean dealCategoryBean;

	private String highlight;

	private Integer maxAvailable;

	private Integer purchaseQuantityPerUser;

	private String diadContractNumber;

	private Integer diadContractItem;

	private Integer status;

	private Integer syncstate;

	private String bookCode;

	private String artID;

	private String createBy;

	private RedemptionBean redemption;

	// Recommend by NC API
	private String categoryRecommend;

	@XmlElement(name = "redemptionLocation")
	private List<LocationBean> redemptionLocationInfoBean;

	private String googleofferMerchantID;

	/**
	 * @return the dealID
	 */
	public Integer getDealID() {
		return dealID == null ? 0 : dealID.intValue();
	}

	/**
	 * @return the merchantBean
	 */
	public MerchantBean getMerchantBean() {
		return merchantBean;
	}

	/**
	 * @param merchantBean
	 *            the merchantBean to set
	 */
	public void setMerchantBean(MerchantBean merchantBean) {
		this.merchantBean = merchantBean;
	}

	/**
	 * @return the dealEngineID
	 */
	public int getDealEngineID() {
		return dealEngineID == null ? -1 : this.dealEngineID.intValue();
	}

	/**
	 * @param dealEngineID
	 *            the dealEngineID to set
	 */
	public void setDealEngineID(Integer dealEngineID) {
		this.dealEngineID = dealEngineID;
	}

	/**
	 * @return the deDealID
	 */
	public int getDeDealID() {
		return deDealID == null ? -1 : this.deDealID.intValue();
	}

	/**
	 * @param deDealID
	 *            the deDealID to set
	 */
	public void setDeDealID(Integer deDealID) {
		this.deDealID = deDealID;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the price
	 */
	public Double getPrice() {
		return price;
	}

	/**
	 * @param price
	 *            the price to set
	 */
	public void setPrice(Double price) {
		this.price = price;
	}

	/**
	 * @return the value
	 */
	public Double getValue() {
		return value;
	}

	/**
	 * @param value
	 *            the value to set
	 */
	public void setValue(Double value) {
		this.value = value;
	}

	/**
	 * @return the startDate
	 */
	public String getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate
	 *            the startDate to set
	 */
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setStartDate(Date startDate) {
		if (startDate == null)
			return;
		this.startDate = DealBean.UI_DATE_FORMAT.format(startDate);
	}

	/**
	 * @return the endDate
	 */
	public String getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate
	 *            the endDate to set
	 */
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public void setEndDate(Date endDate) {
		if (endDate == null)
			return;
		this.endDate = DealBean.UI_DATE_FORMAT.format(endDate);
	}
	
	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the finePrint
	 */
	public String getFinePrint() {
		return finePrint;
	}

	/**
	 * @param finePrint
	 *            the finePrint to set
	 */
	public void setFinePrint(String finePrint) {
		this.finePrint = finePrint;
	}

	/**
	 * @return the imageLink
	 */
	public String getImageLink() {
		return imageLink;
	}

	/**
	 * @param imageLink
	 *            the imageLink to set
	 */
	public void setImageLink(String imageLink) {
		this.imageLink = imageLink;
	}

	/**
	 * @return the applicableMarketID
	 */
	public int getApplicableMarketID() {
		return applicableMarketID == null ? -1 : applicableMarketID.intValue();
	}

	/**
	 * @param applicableMarketID
	 *            the applicableMarketID to set
	 */
	public void setApplicableMarketID(Integer applicableMarketID) {
		this.applicableMarketID = applicableMarketID;
	}

	/**
	 * @return the dealCategoryBean
	 */
	public DealCategoryBean getDealCategoryBean() {
		return dealCategoryBean;
	}

	/**
	 * @param dealCategoryBean
	 *            the dealCategoryBean to set
	 */
	public void setDealCategoryBean(DealCategoryBean dealCategoryBean) {
		this.dealCategoryBean = dealCategoryBean;
	}

	/**
	 * @return the highlight
	 */
	public String getHighlight() {
		return highlight;
	}

	/**
	 * @param highlight
	 *            the highlight to set
	 */
	public void setHighlight(String highlight) {
		this.highlight = highlight;
	}

	/**
	 * @return the maxAvailable
	 */
	public Integer getMaxAvailable() {
		return maxAvailable;
	}

	/**
	 * @param maxAvailable
	 *            the maxAvailable to set
	 */
	public void setMaxAvailable(Integer maxAvailable) {
		this.maxAvailable = maxAvailable;
	}

	/**
	 * @return the purchaseQuantityPerUser
	 */
	public Integer getPurchaseQuantityPerUser() {
		return purchaseQuantityPerUser;
	}

	/**
	 * @param purchaseQuantityPerUser
	 *            the purchaseQuantityPerUser to set
	 */
	public void setPurchaseQuantityPerUser(Integer purchaseQuantityPerUser) {
		this.purchaseQuantityPerUser = purchaseQuantityPerUser;
	}

	/**
	 * @return the diadContractNumber
	 */
	public String getDiadContractNumber() {
		return diadContractNumber;
	}

	/**
	 * @param diadContractNumber
	 *            the diadContractNumber to set
	 */
	public void setDiadContractNumber(String diadContractNumber) {
		this.diadContractNumber = diadContractNumber;
	}

	/**
	 * @return the diadContractItem
	 */
	public Integer getDiadContractItem() {
		return diadContractItem;
	}

	/**
	 * @param diadContractItem
	 *            the diadContractItem to set
	 */
	public void setDiadContractItem(Integer diadContractItem) {
		this.diadContractItem = diadContractItem;
	}

	/**
	 * @return the status
	 */
	public Integer getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(Integer status) {
		this.status = status;
	}

	/**
	 * @return the syncstate
	 */
	public Integer getSyncstate() {
		return syncstate;
	}

	/**
	 * @param syncstate
	 *            the syncstate to set
	 */
	public void setSyncstate(Integer syncstate) {
		this.syncstate = syncstate;
	}

	/**
	 * @param dealID
	 *            the dealID to set
	 */
	public void setDealID(Integer dealID) {
		this.dealID = dealID;
	}

	/**
	 * @return the applicableMarketName
	 */
	public String getApplicableMarketName() {
		return applicableMarketName;
	}

	/**
	 * @param applicableMarketName
	 *            the applicableMarketName to set
	 */
	public void setApplicableMarketName(String applicableMarketName) {
		this.applicableMarketName = applicableMarketName;
	}

	public String getCity() {
		if (getMerchantBean() != null
				&& getMerchantBean().getMerchantContactInfoBean() != null) {
			return getMerchantBean().getMerchantContactInfoBean().getCity();
		}
		return null;
	}

	public String getMerchantName() {
		if (getMerchantBean() != null) {
			return getMerchantBean().getName();
		}
		return null;
	}

	public String getCategory() {
		if (getDealCategoryBean() != null) {
			return getDealCategoryBean().getName();
		}
		return null;
	}

	private static final String LINE_SEPARATOR = System
			.getProperty("line.separator");

	public String toString() {
		StringBuffer sb = new StringBuffer("--- Deal Start ---");

		sb.append(LINE_SEPARATOR);
		sb.append("DealID: ");
		sb.append(getDealID());

		sb.append(LINE_SEPARATOR);
		sb.append("Weforia: ");
		sb.append(getDeDealID());

		sb.append(LINE_SEPARATOR);
		sb.append("Title: ");
		sb.append(getTitle());

		sb.append(LINE_SEPARATOR);
		sb.append("City: ");
		sb.append(getCity());

		sb.append(LINE_SEPARATOR);
		sb.append("Start Date: ");
		sb.append(getStartDate());

		sb.append(LINE_SEPARATOR);
		sb.append("Merchant: ");
		sb.append(getMerchantName());

		sb.append(LINE_SEPARATOR);
		sb.append("Category: ");
		sb.append(getCategory());

		sb.append(LINE_SEPARATOR);
		sb.append("--- Deal End ---");
		return sb.toString();
	}

	/**
	 * @param bookCode
	 *            the bookCode to set
	 */
	public void setBookCode(String bookCode) {
		this.bookCode = bookCode;
	}

	/**
	 * @return the bookCode
	 */
	public String getBookCode() {
		return bookCode;
	}

	/**
	 * @param artID
	 *            the artID to set
	 */
	public void setArtID(String artID) {
		this.artID = artID;
	}

	/**
	 * @return the artID
	 */
	public String getArtID() {
		return artID;
	}

	/**
	 * @param redemptionLocationInfoBean
	 *            the redemptionLocationInfoBean to set
	 */
	public void setRedemptionLocationInfoBean(
			List<LocationBean> redemptionLocationInfoBean) {
		this.redemptionLocationInfoBean = redemptionLocationInfoBean;
	}

	/**
	 * @return the redemptionLocationInfoBean
	 */
	public List<LocationBean> getRedemptionLocationInfoBean() {
		return redemptionLocationInfoBean;
	}

	public boolean hasRedemptionLocation() {
		return getRedemptionLocationInfoBean() != null
				&& getRedemptionLocationInfoBean().size() > 0;
	}

	public void addRedemptionLocation(LocationBean redemptionLocationInfo) {
		if (this.getRedemptionLocationInfoBean() == null) {
			this.redemptionLocationInfoBean = new ArrayList<LocationBean>(0);
		}
		this.redemptionLocationInfoBean.add(redemptionLocationInfo);
	}

	/**
	 * @param categoryRecommend
	 *            the categoryRecommend to set
	 */
	public void setCategoryRecommend(String categoryRecommend) {
		this.categoryRecommend = categoryRecommend;
	}

	/**
	 * @return the categoryRecommend
	 */
	public String getCategoryRecommend() {
		return categoryRecommend;
	}

	public Integer getDealCategoryID() {
		return this.dealCategoryBean == null ? -1 : this.dealCategoryBean
				.getDealCategoryID();
	}
	/**
	 * @param createBy
	 *            the createBy to set
	 */
	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	/**
	 * @return the createBy
	 */
	public String getCreateBy() {
		return createBy;
	}

	/**
	 * @return the googleofferMerchantID
	 */
	public String getGoogleofferMerchantID() {
		return googleofferMerchantID;
	}

	/**
	 * @param googleofferMerchantID the googleofferMerchantID to set
	 */
	public void setGoogleofferMerchantID(String googleofferMerchantID) {
		this.googleofferMerchantID = googleofferMerchantID;
	}

	public boolean isPersistent() {
		return getDealID() > 0;
	}

	/**
	 * @return the description2
	 */
	public String getDescription2() {
		return description2;
	}

	/**
	 * @param description2 the description2 to set
	 */
	public void setDescription2(String description2) {
		this.description2 = description2;
	}

	/**
	 * @return the providerLogo
	 */
	public String getProviderLogo() {
		return providerLogo;
	}

	/**
	 * @param providerLogo the providerLogo to set
	 */
	public void setProviderLogo(String providerLogo) {
		this.providerLogo = providerLogo;
	}

	/**
	 * @param redemption the redemption to set
	 */
	public void setRedemption(RedemptionBean redemption) {
		this.redemption = redemption;
	}

	/**
	 * @return the redemption
	 */
	public RedemptionBean getRedemption() {
		return redemption;
	}
	
	public boolean isSyndicated() {
		return getStatus().equals(DealStatus.On);
	}

}
